package com.cq.controller;

import com.cq.pojo.Permission;
import com.cq.pojo.Result;
import com.cq.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.LinkedList;
import java.util.List;

/**
 * @Author: 悠久的苍穹
 * @Date: 2021/2/7 19:53
 * @Description: 权限控制层
 * @Version: 1.0
 */
@Controller
public class PermissionController {

    @Autowired
    PermissionService permissionService;

    @ResponseBody
    @GetMapping("/permission/getPermission")
    public Result getPermission(){
        Result result = new Result();
        List<Permission> permissionList = permissionService.getPermissions();
        List<Permission> list = new LinkedList<>();
        for (int i = 0;i<permissionList.size();i++){
            if (permissionList.get(i).getParent_id() == 0){
                List<Permission> list2 = new LinkedList<>();
                for (int j = 0;j<permissionList.size();j++){
                    if (permissionList.get(j).getParent_id() == permissionList.get(i).getId()){
                        List<Permission> list3 = new LinkedList<>();
                        for (int k = 0;k<permissionList.size();k++){
                            if (permissionList.get(k).getParent_id().equals(permissionList.get(j).getId())){
                                list3.add(permissionList.get(k));
                            }
                        }
                        permissionList.get(j).setChildren(list3);
                        list2.add(permissionList.get(j));
                    }
                }
                permissionList.get(i).setChildren(list2);
                list.add(permissionList.get(i));
            }
        }
        result.setData(list);
        result.setCode(0);
        return result;
    }

    @ResponseBody
    @GetMapping("/permission/getPermission/{id}")
    public Result getPermissionByAdminId(@PathVariable("id") Integer id){
        Result result = new Result();
//        System.out.println(id);
        List<Permission> permissionList = permissionService.getPermissions();
//        System.out.println(permissionList);
        List<Permission> list = new LinkedList<>();
        List<Permission> permissions_ids = permissionService.getPermissionsById(id);
//        System.out.println(permissions_ids);
        List<Integer> ids = new LinkedList<>();
        for (Permission p:permissions_ids){
            ids.add(p.getId());
        }
        for (int i = 0;i<permissionList.size();i++){
            if (permissionList.get(i).getParent_id() == 0){
                List<Permission> list2 = new LinkedList<>();
                for (int j = 0;j<permissionList.size();j++){
                    if (permissionList.get(j).getParent_id().equals(permissionList.get(i).getId())){
                        List<Permission> list3 = new LinkedList<>();
                        for (int k = 0;k<permissionList.size();k++){
                            if (permissionList.get(k).getParent_id().equals(permissionList.get(j).getId())){
                                if (ids.contains(permissionList.get(k).getId())){
                                    permissionList.get(k).setChecked(true);
                                }
                                list3.add(permissionList.get(k));
                            }
                        }
                        permissionList.get(j).setChildren(list3);
                        if (permissionList.get(j).getChildren().isEmpty() && ids.contains(permissionList.get(j).getId())){
                            permissionList.get(j).setChecked(true);
                        }
                        list2.add(permissionList.get(j));
                    }
                }
                permissionList.get(i).setChildren(list2);
                list.add(permissionList.get(i));
            }
        }
        result.setData(list);
        result.setCode(0);
        return result;
    }

    @ResponseBody
    @PutMapping(value = "/role/updatePermission/{id}")
    public Result updateMenu(@PathVariable("id") String id,
                              @RequestParam("role_id") String role_id) {
        Result result = new Result();
        permissionService.updatePermission(role_id,id);
        result.setCode(200);
        return result;
    }
}
